# @Time : 2020/8/28 11:11
# @Author : Fioman 
# @Phone : 13149920693
import numpy as np

x = np.arange(0, 10)
print(x)
xx = np.piecewise(x, [x < 4, x >= 6], [-1, 1])  # 满足两个条件
print(xx)


# 如果不满足,则被置为0,如果满足条件,则被置为-1,不满足条件,被置为1
# 两个条件是独立的,分别对应的值是-1和1

# 可以用函数来替换掉
def func2(y):
    return 1

def func1(y):
    return -1

xx = np.piecewise(x, [x > 3, x > 5], [func1, func2])
print(xx)

def func3(y):
    return y ** 2
def func4(y):
    return np.square(y) * y
xx = np.piecewise(x, [x > 2, x < 3], [func3, func4])
print(xx)

print(x)
# 使用匿名函数,相当于是做了两次运算,后面那个结果会覆盖掉前面的那个结果
xx = np.piecewise(x, [x < 6, x >= 6], [lambda x: x ** 8, lambda x: np.sqrt(x)])
print(xx)
